package com.ruiben.mall.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruiben.mall.IRechargeCardService;
import com.ruiben.mall.mapper.RechargeCardMapper;
import com.ruiben.mall.mapper.RechargeLogMapper;
import com.ruiben.mall.mapper.UserMapper;
import com.ruiben.mall.pojo.Admin;
import com.ruiben.mall.pojo.RechargeCard;
import com.ruiben.mall.pojo.RechargeLog;
import com.ruiben.mall.pojo.User;
import com.ruiben.mall.utils.CardPassUtils;
import com.ruiben.mall.utils.DateUtil;
import com.ruiben.mall.utils.PagedResult;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.sql.Time;
import java.util.*;


@Service
public class RechargeCardServiceImpl implements IRechargeCardService {

    @Autowired
    private Sid sid;

    @Autowired
    private RechargeCardMapper rechargeCardMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RechargeLogMapper rechargeLogMapper;



    /**
     * 获取 充值记录
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PagedResult getRechargeLogList(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<RechargeLog> userRechargeLogList = rechargeLogMapper.getUserRechargeLogList();
        PageInfo<RechargeLog> pageList = new PageInfo<>(userRechargeLogList);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(userRechargeLogList);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }


    /**
     * 用户充值
     * @param cardNumber
     * @param cardPass
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean userRecharge(String rechargeUser,String cardNumber,String cardPass){
        //查找对应的用户
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",rechargeUser);
        User user = userMapper.selectOneByExample(example);
        if (user==null){
            return false;
        }

        //查找是否有对应的卡号
        RechargeCard card = rechargeCardMapper.selectByPrimaryKey(cardNumber);
        if (card==null){
            return false;
        }

        if (card.getRechargeStatus()){
            return false;
        }

        if (cardPass.equals(CardPassUtils.parseJWTPassword(card.getCardPassword()).getId())){
            //用户充值
            //查找相关用户的余额
            long total = user.getUserMoney() + (card.getMoney().longValue());
            user.setUserMoney(total);
            int i = userMapper.updateByPrimaryKeySelective(user);

            //把充值卡更新为已充值过
            card.setRechargeStatus(true);
            rechargeCardMapper.updateByPrimaryKeySelective(card);

            RechargeLog rechargeLog = new RechargeLog();
            rechargeLog.setId(sid.nextShort());
            rechargeLog.setRechargeMoney(card.getMoney());
            rechargeLog.setRechargeTime(new Date());
            rechargeLog.setUserid(user.getId());
            rechargeLog.setCardId(card.getCardNumber());
            rechargeLogMapper.insert(rechargeLog);

            return i>0;
        } else {
            return false;
        }

    }
}
